package com.ruoyi.carton.controller;


import com.ruoyi.carton.domain.bo.OrderInboundBO;
import com.ruoyi.carton.domain.vo.OrderInboundListVO;
import com.ruoyi.carton.domain.vo.OrderInboundUpdateVO;
import com.ruoyi.carton.service.OrderInboundService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/inbound")
public class OrderInboundController extends BaseController {

    @Autowired
    private OrderInboundService orderInboundService;

    /**
     * 查询入库单详情
     *
     * @param id 入库单ID
     * @return 入库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:inbound:query')")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Integer id) {
        OrderInboundBO bo = orderInboundService.getInboundById(id);
        return success(bo);
    }

    /**
     * 分页查询入库单列表（支持基于OrderInboundListVO的查询条件）
     *
     * @param queryVO 查询条件封装对象
     * @return 分页列表数据
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:inbound:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInboundListVO queryVO) {
        startPage(); // 启用分页
        List<OrderInboundBO> list = orderInboundService.selectBOList(queryVO);
        return getDataTable(list);
    }

    /**
     * 删除入库单，删除前判断订单状态是否允许删除，删除后回退订单状态
     *
     * @param id 入库单ID
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:inbound:remove')")
    @Log(title = "入库单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        try {
            orderInboundService.deleteInboundById(id);
            return AjaxResult.success("删除成功并回退订单状态");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改入库单信息
     *
     * @param orderInboundUpdateVO 入库单更新视图对象
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:inbound:edit')")
    @Log(title = "入库单管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@RequestBody OrderInboundUpdateVO orderInboundUpdateVO) {
        try {
            boolean updated = orderInboundService.updateOrderInboundById(orderInboundUpdateVO);
            return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败，入库单不存在");
        } catch (Exception e) {
            return AjaxResult.error("更新失败：" + e.getMessage());
        }
    }
}
